const router = require('koa-router')()
const sqlQuery = require('../db.js');

router.prefix("/")

router.get('/paper', async (ctx, next)=> {
  try {
    let sql = `select * from goods,cates where goods.gcateid=cates.cid`;
    let result = await sqlQuery(sql, []);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})

router.post('/paper', async (ctx, next)=> {
  try {
    let { gid,gname,gdate,gcateid,ginfo } = ctx.request.body;
    let sql = `insert into goods(gid,gname,gdate,ginfo,gcateid) values(?,?,?,?,?)`;
    let result = await sqlQuery(sql, [gid,gname,gdate,ginfo,gcateid]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})

router.get('/course', async (ctx, next)=> {
  try {
    let sql = `SELECT * FROM cates`;
    let result = await sqlQuery(sql, []);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})

module.exports = router
